﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ParallelRuntimeLibrary
{
    /// <summary>
    /// Parallel loop state content Break method that allow 
    /// </summary>
    public sealed class ParallelLoopState
    {

        internal Task[] mTasks;
        internal Thread[] mThreads;

        /*///////////////////////////////////////////////////////////////////////////////////////////////*/

        public ParallelLoopState()
        {

        }

        public ParallelLoopState(Task[] tasks)
        {
            mTasks = tasks;
        }

        public ParallelLoopState(Thread[] threads)
        {
            mThreads = threads;
        }

        public static ParallelLoopState CreateSimple()
        {
            return new ParallelLoopState();
        }

        /*///////////////////////////////////////////////////////////////////////////////////////////////*/

        public void Break()
        {
            if (mThreads != null)
            {
                for (int i = 0; i < mThreads.Length; i++)
                    mThreads[i].Abort();
            }
            else
            {
                if (mTasks.Length == 1)
                    Thread.CurrentThread.Abort();
                else
                {
                    for (int i = 0; i < mTasks.Length; i++)
                        TaskManager.Abort(mTasks[i]);
                }
            }
        }
    }
}
